package exttable.timetable.groupshchemapanel;

import dbutils.DbUtils;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 20.09.2010
 * Time: 23:30:50
 * To change this template use File | Settings | File Templates.
 */
public class FillGroupsInSchema {

    public FillGroupsInSchema() {
        JFrame frame = new JFrame();
        JTable table = new JTable(new FillGroupsInSchemaTableModel());
        table.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(getComboBox()));
        JScrollPane scroll = new JScrollPane(table);
        frame.add(scroll);
        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    private JComboBox getComboBox() {
        Collection<String> items = new ArrayList<String>();
        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement("select  group_code from groups");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                items.add(rs.getString("group_code"));
            }
            ps.close();
            c.close();
            return new JComboBox(items.toArray());
        }
        catch (Exception e) {
            throw new IllegalArgumentException("erro");
        }
    }
}
